package cn.com.fstar.framework.admin.controller;

import cn.com.fstar.core.CodeConstant;
import cn.com.fstar.core.pageModel.Organization;
import cn.com.fstar.core.pageModel.Tree;
import cn.com.fstar.core.util.LogUtil;
import cn.com.fstar.core.util.StringUtil;
import cn.com.fstar.framework.admin.service.OrganizationService;
import cn.com.fstar.framework.base.controller.BaseController;
import cn.com.fstar.framework.base.model.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 该模块主要为了展示图表例子，方便其他程序员学习使用
 * 
 * @author admin/Organization/treeGrid
 */

@Controller
@RequestMapping("/admin/organization")
public class OrganizationController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 跳转到图表页面
     * 
     * @return
     */
    @RequestMapping("/treeGrid")
    public String treeGrid() {
        return "/admin/organization/organization";
    }

    /**
     * 获取组织机构list
     * 
     * @author Guodong 2015-06-11 15:56:03
     * @param navigation
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadTreeGrid")
    public List<Organization> loadTreeGrid(Organization Organization, HttpServletRequest request, HttpSession session) {
        return organizationService.organizationTreeGrid(Organization);
    }

    /**
     * 跳转到添加页面
     * 
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "/admin/organization/organizationAdd";
    }

    /**
     * 添加字典
     * 
     * @author Guodong 2015-06-11 19:20:21
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public JsonResult add(Organization org, HttpServletRequest request) {

        // 定义easyUI Json对象
        JsonResult j = new JsonResult();

        if (org == null) {
            j.setSuccess(false);
            j.setMsg("请填写组织机构信息！");
            return j;
        }

        // 调用Service方法
        try {
            org.setId(StringUtil.generateUUID());
            String msg = organizationService.add(org);
            if (msg == null) {
                j.setSuccess(true);
                j.setMsg("新组织机构添加成功！");
                LogUtil.adminLogs("新组织机构添加成功。", CodeConstant.ADD_OPERATION, request);

            } else {
                j.setSuccess(false);
                j.setMsg(msg);
                LogUtil.adminLogs("新组织机构添加失败。", CodeConstant.ADD_OPERATION, request);
            }
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("操作异常，请联系系统维护人员！");
            LogUtil.adminLogs("新组织机构添加失败。", CodeConstant.ADD_OPERATION, request);
            e.printStackTrace();
        }

        return j;
    }

    @ResponseBody
    @RequestMapping("/organizationComboTree")
    public List<Tree> organizationComboTree() {
        return organizationService.organizationComboTree();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(String id, HttpServletRequest request) {

        // 定义easyUI Json对象
        JsonResult j = new JsonResult();

        // 调用Service方法
        try {

            String msg = organizationService.deleteOrganization(id);
            if (msg == null) {
                j.setSuccess(true);
                j.setMsg("删除组织机构成功！");
                LogUtil.adminLogs("新组织机构删除成功", CodeConstant.DELETE_OPERATION, request);

            } else {
                j.setSuccess(false);
                j.setMsg(msg);
                LogUtil.adminLogs("删除成功", CodeConstant.DELETE_OPERATION, request);
            }
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("操作异常，请联系系统维护人员！");
            LogUtil.adminLogs("新组织机构删除失败", CodeConstant.DELETE_OPERATION, request);
        }

        return j;
    }

    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request, String id) {
        Organization d = organizationService.getOrganization(id);
        request.setAttribute("organization", d);
        return "/admin/organization/organizationEdit";
    }

    /**
     * 修改字典
     */
    @ResponseBody
    @RequestMapping("/edit")
    public JsonResult edit(Organization Organization, HttpServletRequest request) {

        // 定义easyUI Json对象
        JsonResult j = new JsonResult();

        if (Organization == null) {

            j.setSuccess(false);
            j.setMsg("保存对象为空，请重新操作！");
            LogUtil.adminLogs("组织机构修改失败", CodeConstant.EDIT_OPERATION, request);
            return j;
        }
        // 调用Service方法
        try {
            String msg = organizationService.edit(Organization);
            if (msg == null) {
                j.setSuccess(true);
                j.setMsg("组织机构修改成功！");
                LogUtil.adminLogs("组织机构修改成功：", CodeConstant.EDIT_OPERATION, request);

            } else {
                j.setSuccess(false);
                j.setMsg(msg);
                LogUtil.adminLogs("组织机构修改失败：" + msg, CodeConstant.EDIT_OPERATION, request);
            }

        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("操作异常，请联系系统维护人员！");
            LogUtil.adminLogs("组织机构修改失败", CodeConstant.EDIT_OPERATION, request);
        }

        return j;
    }

}
